package com.pdemo.front.resource;

import java.text.SimpleDateFormat;

import javax.ws.rs.ext.ContextResolver;
import javax.ws.rs.ext.Provider;

import org.glassfish.jersey.jackson.JacksonFeature;
import org.glassfish.jersey.server.ResourceConfig;
import org.glassfish.jersey.server.filter.RolesAllowedDynamicFeature;
import org.glassfish.jersey.server.spring.scope.RequestContextFilter;

import com.fasterxml.jackson.databind.MapperFeature;
import com.fasterxml.jackson.databind.ObjectMapper;

public class ApplicationResourceConfig extends ResourceConfig{
	public ApplicationResourceConfig() {
		register(RequestContextFilter.class);
		register(RolesAllowedDynamicFeature.class);
		register(ApplicationMapperProvider.class);
		register(JacksonFeature.class);
	}
	
	
	@Provider
	public static class ApplicationMapperProvider implements ContextResolver<ObjectMapper>
	{

		final ObjectMapper defaultObjectMapper;

		public ApplicationMapperProvider()
		{
			defaultObjectMapper = createDefaultMapper();
		}

		@Override
		public ObjectMapper getContext(Class<?> type)
		{
			return defaultObjectMapper;
		}
	}

	private static ObjectMapper createDefaultMapper()
	{
		final ObjectMapper result = new ObjectMapper();
		result.configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false);
		result.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
		return result;
	}

}
